﻿<?xml version="1.0" encoding="utf-8"?><Articles><Article><ArticlesID>627</ArticlesID><Title>Sử dụng String.Split v&amp;#224; Regular Expression để t&amp;#225;ch c&amp;#225;c chuỗi</Title><Body>&amp;lt;p&amp;gt;Giả sử bạn c&amp;amp;oacute; 1 chuỗi:&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: csharp;&amp;quot;&amp;gt;
string strTest = &amp;amp;quot;| Laocai | a1@vn.com;a2@vn.com;a3@vn.com;|&amp;amp;quot;; &amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Mục đ&amp;amp;iacute;ch của bạn b&amp;amp;acirc;y giờ l&amp;amp;agrave; t&amp;amp;aacute;ch từng phần trong chuỗi n&amp;amp;agrave;y ra để lấy về&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;1. &amp;amp;quot;Laocai&amp;amp;quot;:&amp;amp;nbsp;Tỉnh th&amp;amp;agrave;nh phố&amp;lt;br /&amp;gt;
2. &amp;amp;quot;a1[at]vn.com,....&amp;amp;quot; l&amp;amp;acirc;&amp;amp;yacute; về chuỗi c&amp;amp;aacute;c email&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: csharp;&amp;quot;&amp;gt;
string[] strArr = null;
string strTest = &amp;amp;quot;| Laocai | a1@vn.com;a2@vn.com;a3@vn.com;|&amp;amp;quot;;
char[] splitter  = {'|'};
strArr = strTest.Split(splitter);

//lay tinh thanh
MessageBox.Show(strArr[1].ToString());
//lay ve chuoi email dang sau
MessageBox.Show(strArr[2].ToString()); &amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;&amp;amp;nbsp;&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: csharp;&amp;quot;&amp;gt;
//lay tung email ngan cach nhau boi dau ;
//1. Dung Split

string[] strArr2 = null;
char[] splitter2 = { ';' };
strArr2 = strArr[2].ToString().Split(splitter2);
for (int i = 0; i &amp;amp;lt;= strArr2.Length-1; i++)
{
     if (strArr2[i].ToString().Length &amp;amp;gt; 1)
     {
        MessageBox.Show(strArr2[i].ToString());
     }
}

//2. Dung Regular Expression (cai nay co ve chuan hon vi no validate email luon)

string patten = @&amp;amp;quot;\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*&amp;amp;quot;;
Regex reg = new Regex(patten);
Match matchResult = reg.Match(strArr[2].ToString());
while(matchResult.Success)
{
      MessageBox.Show(matchResult.Value);
      matchResult = matchResult.NextMatch();
} &amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Theo r3z0lee &amp;amp;amp;&amp;amp;nbsp;Justin @ aspvn.net&amp;lt;/p&amp;gt;</Body><CommentEnabled>True</CommentEnabled><ViewCount>0</ViewCount><ReleaseDate>11/21/2009 9:57:06 PM</ReleaseDate><TotalComment>0</TotalComment><IsMedia>False</IsMedia><IsPicture>False</IsPicture><IsVote>True</IsVote><ArticlePassword /><PostVisible>0</PostVisible><ArticlePath>/2009/11/21/Su-dung-String-Split-va-Regular-Expression-de-tach-cac-chuoi-273</ArticlePath><Approved>True</Approved><ImagePath /><CategoryId>43</CategoryId><ArrayCatID>43|25</ArrayCatID></Article></Articles>